/** * Blog post detail page — renders a single published blog post by its slug. * * - Fetches the post from the Brainerce SDK (returns null on 404) * - Generates full SEO metadata (title, description, OG image) * - Injects Article JSON-LD for Google rich snippets * - Body HTML is sanitized before rendering * * One implementation for single- and multi-language stores: `locale` exists only * under the `[locale]` segment and is `undefined` otherwise. Every string comes * from the `blog` message namespace in the request locale; none is a * `locale === 'he'` ternary, which gave every third language English and forced * `dir="ltr"` onto Arabic. * * Security: always pass blog content through sanitizeHtml before dangerouslySetInnerHTML. */ import * as React from 'react'; import type { Metadata } from 'next'; import { notFound, permanentRedirect } from 'next/navigation'; import { Link } from '@/core/lib/navigation'; import { CdnImage as Image } from '@/ui/shared/cdn-image'; import { ArticleJsonLd } from '@/components/seo/article-json-ld'; import { getCanonicalSiteUrl } from '@/core/lib/site-url'; import { defaultLocale } from '@/i18n'; import { getServerClient, fetchStoreInfo, getBlogPostOnce } from '@/core/lib/brainerce.server'; import { localePath } from '@/core/lib/locale-path'; import { getServerNamespace } from '@/core/lib/server-messages'; import { sanitizeHtml } from '@/core/lib/sanitize'; import { decodeSlug } from '@/core/lib/utils'; type PageProps = { params: Promise<{ locale?: string; slug: string }>; }; export async function generateMetadata({ params }: PageProps): Promise { const { locale, slug: rawSlug } = await params; const slug = decodeSlug(rawSlug); const [post, storeInfo] = await Promise.all([ getBlogPostOnce(locale, slug).catch(() => null), fetchStoreInfo(locale), ]); if (!post) return { title: slug }; // Canonical + hreflang. Blog URLs use the same slug across locales (only // the locale prefix changes) — the same convention getBlogSitemapEntries // emits, so the alternates always match real, crawlable URLs. const supportedLocales = storeInfo?.i18n?.supportedLocales ?? []; const defaultLoc = storeInfo?.i18n?.defaultLocale ?? storeInfo?.language ?? ''; const SITE_URL = await getCanonicalSiteUrl(); const canonicalPath = localePath(locale, `/blog/${post.slug}`); const hreflangLanguages = supportedLocales.length > 1 ? Object.fromEntries([ ...supportedLocales.map((loc) => [ loc, loc === defaultLoc ? `${SITE_URL}/blog/${post.slug}` : `${SITE_URL}/${loc}/blog/${post.slug}`, ]), ['x-default', `${SITE_URL}/blog/${post.slug}`], ]) : undefined; return { title: post.seoTitle ?? post.title, description: post.seoDescription ?? post.excerpt, alternates: { canonical: canonicalPath, ...(hreflangLanguages ? { languages: hreflangLanguages } : {}), }, // `openGraph` replaces the root layout's object wholesale rather than merging // into it, so `siteName` is repeated here. openGraph: { title: post.seoTitle ?? post.title, description: post.seoDescription ?? post.excerpt ?? undefined, ...(storeInfo?.name ? { siteName: storeInfo.name } : {}), type: 'article', publishedTime: post.publishedAt ?? undefined, authors: post.author ? [post.author] : undefined, images: post.ogImageUrl ? [{ url: post.ogImageUrl }] : post.coverImageUrl ? [{ url: post.coverImageUrl }] : undefined, }, }; } export default async function BlogPostPage({ params }: PageProps) { const { locale, slug: rawSlug } = await params; const slug = decodeSlug(rawSlug); const client = await getServerClient(locale); const [post, storeInfo] = await Promise.all([ // getPost returns null only on a genuine 404; any other API failure (a // 403 from the channel's origin check, a network error) throws and // surfaces as the error it is instead of masquerading as a missing post. getBlogPostOnce(locale, slug), client.getStoreInfo().catch(() => null), ]); if (!post) { // The slug may have been renamed — 301 to the current slug so the old // URL keeps its ranking; genuine unknowns fall through to the 404 page. // Default locale stays unprefixed (matches the sitemap/hreflang convention). const redirect = await client.resolveSlugRedirect('blog', slug); if (redirect) { permanentRedirect(localePath(locale, `/blog/${redirect.currentSlug}`)); } notFound(); } const [SITE_URL, strings, seo] = await Promise.all([ getCanonicalSiteUrl(), getServerNamespace('blog', locale), getServerNamespace('seo', locale), ]); // The same URL the canonical tag names (locale prefix included), so the // Article JSON-LD cannot point at a different page than the page declares. const articleUrl = `${SITE_URL}${localePath(locale, `/blog/${post.slug}`)}`; const breadcrumbs = [ { name: seo.home ?? '', url: `${SITE_URL}${localePath(locale, '/')}` }, { name: seo.blog ?? '', url: `${SITE_URL}${localePath(locale, '/blog')}` }, { name: post.title, url: articleUrl }, ]; return (
{/* Back link */} {strings.backToBlog} {/* Category */} {post.category && (
{post.category}
)} {/* Title */}

{post.title}

{/* Meta row: author + date + tags */}
{post.author && {post.author}} {post.publishedAt && ( )} {post.tags && post.tags.length > 0 && (
{post.tags.map((tag) => ( {tag} ))}
)}
{/* Cover image */} {post.coverImageUrl && (
{post.coverImageAlt
)} {/* Excerpt */} {post.excerpt && (

{post.excerpt}

)} {/* Body */}
); }